<template>
  <div class="app-container">
    <!-- <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="机构ID" prop="infoId">
        <el-input
          v-model="queryParams.infoId"
          placeholder="请输入机构ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form> -->

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['merchant:taskInfo:add']">新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="taskInfoList">
      <el-table-column label="任务ID" align="center" prop="id" />
      <el-table-column label="推送价格" align="center" prop="priceAmount" />
      <el-table-column label="成本/提成价格" align="center" prop="priceAmount" >
       <template slot-scope="scope">
          {{ scope.row.costAmount || '' }} / {{ scope.row.payAmount || '' }}
        </template>
      </el-table-column>
      <el-table-column label="推送数量" align="center" prop="pushNumber" />
      <el-table-column label="任务描述" align="center" prop="descInfo" />
      <el-table-column label="推送时间" align="center">
        <template slot-scope="scope">
          {{ scope.row.starttime || '' }}-{{ scope.row.endtime || '' }}
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-switch v-if="isHavePermission(['merchant:taskInfo:edit'])" v-model="scope.row.status"
            :active-value="1" :inactive-value="0" @change="handleStatusChange(scope.row, $event)"></el-switch>
          <div v-else :style="{ color: scope.row.status === 0 ? 'red' : 'green' }">{{ scope.row.status === 0 ? '关' : '开' }}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleShow(scope.row)">查看</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['merchant:taskInfo:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['merchant:taskInfo:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改商户推送任务对话框 -->
    <AddEditDrawer :infoId="queryParams.infoId" ref="AddEditDrawerRef" @refresh="handleQuery" />
  </div>
</template>

<script>
import {
  listTaskInfo,
  getTaskInfo,
  delTaskInfo,
  addTaskInfo,
  updateTaskInfo,
  changeTaskInfoStatus,
} from "@/api/merchant/taskInfo";
import { isHavePermission } from '@/directive/permission/hasPermi'
import AddEditDrawer from "./components/AddEditDrawer.vue";
export default {
  name: "TaskInfo",
  components: {
    AddEditDrawer,
  },
  data() {
    return {
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商户推送任务表格数据
      taskInfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        infoId: null,
      },
      dynamicParamList: [], // 动态参数信息
      addressInfoList: [], //地址树结构信息
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    isHavePermission,
    /** 查询商户推送任务列表 */
    getList() {
      const id = this.$route.query.id
      this.queryParams.infoId = id;
      if (id) {
        this.loading = true;
        listTaskInfo(this.queryParams).then((response) => {
          this.taskInfoList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      }
    },
    //查看详情
    handleShow(row,){
      const data = row || this.taskInfoList.find(item => item.id == this.ids[0]);
      this.$refs.AddEditDrawerRef.show(data, true);
    },
    // 用户状态修改
    handleStatusChange(row, value) {
      let text = (+value) === 0 ? "停用" : "启用";
      changeTaskInfoStatus({ id: row.id, status: +value }).then(res => {
        this.$modal.msgSuccess(text + "成功");
      }).catch(() => {
        row.status = row.status == "0" ? "1" : "0";
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      // this.reset();
      const data = row || this.taskInfoList.find(item => item.id == this.ids[0]);
      // getTaskInfo(id).then((response) => {
      //   this.form = response.data;
      //   this.open = true;
      //   this.title = "修改商户推送任务";
      // });
      this.$refs.AddEditDrawerRef.show(data);
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$refs.AddEditDrawerRef.show();
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除商户推送任务编号为"' + ids + '"的数据项？')
        .then(function () {
          return delTaskInfo(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => { });
    },
  },
};
</script>
